﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
namespace AirlineSystemApp
{
    public class ScheduledFlight
    {
        private string _FlightNo;

        public string FlightNo
        {
            get { return _FlightNo; }
            set { _FlightNo = value; }
        }
        private DateTime _FlightDate;

        public DateTime FlightDate
        {
            get { return _FlightDate; }
            set { _FlightDate = value; }
        }
        private int _FirstClassSeatsAvailable;

        public int FirstClassSeatsAvailable
        {
            get { return _FirstClassSeatsAvailable; }
            set { _FirstClassSeatsAvailable = value; }
        }
        private int _BusinessClassSeatsAvailable;

        public int BusinessClassSeatsAvailable
        {
            get { return _BusinessClassSeatsAvailable; }
            set { _BusinessClassSeatsAvailable = value; }
        }
        private int _EconomyClassSeatsAvailable;

        public int EconomyClassSeatsAvailable
        {
            get { return _EconomyClassSeatsAvailable; }
            set { _EconomyClassSeatsAvailable = value; }
        }
        public bool UpdateScheduleFlight()
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["AirlineConnectionString"]);
            string sqlUpdate = "Update ScheduledFlights set FlightDate=@flightDate,FirstClassSeatsAvailable=@FirstClassSeatsAvailable,BusinessClassSeatsAvailable=@BusinessClassSeatsAvailable,EconomyClassSeatsAvailable=@EconomyClassSeatsAvailable";
            sqlUpdate += " where FlightNo=@FlightNo";
            try
            {
                
                SqlCommand cmd = new SqlCommand(sqlUpdate, conn);
                cmd.Parameters.AddWithValue("@flightDate", _FlightDate.Date);
                cmd.Parameters.AddWithValue("@FirstClassSeatsAvailable", _FirstClassSeatsAvailable);
                cmd.Parameters.AddWithValue("@BusinessClassSeatsAvailable", _BusinessClassSeatsAvailable );
                cmd.Parameters.AddWithValue("@EconomyClassSeatsAvailable", _EconomyClassSeatsAvailable );
                cmd.Parameters.AddWithValue("@FlightNo", _FlightNo);
                if (conn.State == System.Data.ConnectionState.Closed) conn.Open();
                if (cmd.ExecuteNonQuery() > 0) return true;
                return false;

            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
